package sys.spvisor.core.service.journal;

import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import sys.spvisor.core.dao.journal.TJournalMapper;
import sys.spvisor.core.entity.journal.TJournal;
import sys.spvisor.core.entity.journal.TJournalExample;
import sys.spvisor.core.entity.journal.TJournalExample.Criteria;

@Service
public class JournalService {

	@Autowired
	private TJournalMapper journalDao;

	public List<TJournal> getAllJournalByPageInfo(int size, int start) {
		TJournalExample example = new TJournalExample();
		Criteria criteria = example.createCriteria();
		
		//System.out.println(size+"aaaa" +start);
		
		List<TJournal> journalList = journalDao.getJournalByPageInfo(size, start);

		return journalList;

	}

	public List<TJournal> getJournalByTime(TJournal tJournal) {
		TJournalExample example = new TJournalExample();
		Criteria criteria = example.createCriteria();
		Date date = null;
		if (tJournal.getType() == 0) {
			date = tJournal.getjTime();
			criteria.andJTimeEqualTo(date);
		} else if (tJournal.getType() == 1) {
			date = new Date(tJournal.getjTime().getYear(), 1, 1);
			criteria.andJTimeEqualTo(date);
			criteria.andWeekEqualTo(tJournal.getWeek());
		} else if (tJournal.getType() == 2) {
			date = new Date(tJournal.getjTime().getYear(), tJournal.getjTime().getMonth(), 1);
			criteria.andJTimeEqualTo(date);
		}
		criteria.andPidEqualTo(tJournal.getPid());
		criteria.andTypeEqualTo(tJournal.getType());

		List<TJournal> journalList = journalDao.selectByExample(example);
		return journalList;

	}

	@Transactional
	public int editJournal(TJournal tJournal, MultipartFile file, String filePath) {
		int result = -1;

		TJournal temp = journalDao.selectByPrimaryKey(tJournal.getId());
		String fileName = temp.getFile();
		File tempFile = new File(filePath, fileName);
		if (tempFile.exists()) {
			tempFile.delete();
		}
		if (!tempFile.exists()) {
			try {
				file.transferTo(tempFile);
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}
	
	public int getCountJournal(){
		TJournalExample example = new TJournalExample();
		example.createCriteria();
		return journalDao.countByExample(example);
	}

	@Transactional
	public int insertJournal(TJournal tJournal, MultipartFile file, File targetFile) {
		TJournalExample example = new TJournalExample();
		int result = -1;// -1:失败 0:重复上传 1:成功
		Criteria criteria1 = example.createCriteria();
		Date date = null;
		if (tJournal.getType() == 0) {
			date = tJournal.getjTime();
			criteria1.andJTimeEqualTo(date);
		} else if (tJournal.getType() == 1) {
			date = new Date(tJournal.getjTime().getYear(), 1, 1);
			criteria1.andJTimeEqualTo(date);
			criteria1.andWeekEqualTo(tJournal.getWeek());
		} else if (tJournal.getType() == 2) {
			date = new Date(tJournal.getjTime().getYear(), tJournal.getjTime().getMonth(), 1);
			criteria1.andJTimeEqualTo(date);
		}
		if (journalDao.countByExample(example) <= 0) {
			tJournal.setjTime(date);
			try {
				journalDao.insert(tJournal);
				file.transferTo(targetFile);
				result = 1;
			} catch (Exception e) {
				e.printStackTrace();
				result = -1;
			}

		} else {
			result = 0;
		}

		return result;

	}
}
